//
//  XMUserDetailBannerCell.m
//  StarMeal
//
//  Created by 刘宇航 on 2020/4/1.
//

#import "XMUserDetailBannerCell.h"
#import "XMUserDetailBannerCollectionCell.h"
#import "XMUserModel.h"
#import "XMBannerUserInfoView.h"
#import "XMUserDetailVoiceView.h"

@interface XMUserDetailBannerCell () <UICollectionViewDelegate, UICollectionViewDataSource>

@end

@implementation XMUserDetailBannerCell

- (void)setModel:(XMUserModel *)model {
    _model = model;
    self.voiceView.model = model;
    self.userInfoView.model = model;
    if ([XMUtil isBlankString:model.voice_intro.file_url] || 
        model.voice_intro.voice_length <= 0) {
        self.voiceView.hidden = YES;
    } else {
        self.voiceView.hidden = NO;
    }
    
    self.muteButton.hidden = [XMUtil isBlankString:model.video_cover.file_url];
    [self.dataArray removeAllObjects];
    
    if (![XMUtil isBlankString:model.video_cover.video_cover]) {
        [self.dataArray addObject:model.video_cover.video_cover];
    }
    
    if (![XMUtil isBlankString:model.avatar.file_url]) {
        [self.dataArray addObject:model.avatar.file_url];
    }
    
    [self.collectionView reloadData];
    
    if (self.dataArray.count > 1) {
        self.locationLabel.text = [NSString stringWithFormat:@"%ld/%ld",self.cIndex + 1, self.dataArray.count];
    } else {
        self.locationLabel.hidden = YES;
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setUpSubViews];
    self.cIndex = 0;
    
    XMWeakSelf(self);
    self.voiceView.startBlock = ^{
        XMStrongSelf(self);
        if (self.audioStartBlock) {
            self.audioStartBlock(self);
        }
        self.collectionView.scrollEnabled = NO;
    };
    
    self.voiceView.endBlock = ^{
        XMStrongSelf(self);
        if (self.audioEndBlock) {
            self.audioEndBlock(self);
        }
        self.collectionView.scrollEnabled = YES;
    };
    
    self.userInfoView.guardBlock = ^{
        XMStrongSelf(self);
        if (self.guardBlock) {
            self.guardBlock();
        }
    };
}

- (void)setUpSubViews {
    kCornerRadius(self.locationLabel, 12, 0, [UIColor clearColor]);
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(kScreenWidth, kScreenWidth);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    [self.collectionView setCollectionViewLayout:layout];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.pagingEnabled = YES;
    [self.collectionView registerNib:[UINib nibWithNibName:kStringFromClass(XMUserDetailBannerCollectionCell) bundle:nil] forCellWithReuseIdentifier:kStringFromClass(XMUserDetailBannerCollectionCell)];
    
    [self.contentView addSubview:self.userInfoView];
    [self.userInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 66));
        make.bottom.mas_equalTo(self.contentView);
        make.centerX.equalTo(self.contentView);
    }];
    
    [self.contentView addSubview:self.voiceView];
    
    [self.voiceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView);
        make.top.equalTo(self.contentView).offset(94);
        make.size.mas_equalTo(CGSizeMake(75, 31));
    }];
}

#pragma mark - UICollectionViewDelegate & UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMUserDetailBannerCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kStringFromClass(XMUserDetailBannerCollectionCell) forIndexPath:indexPath];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:self.dataArray[indexPath.item]] placeholderImage:kDefaultCoverImg];

    if (self.dataArray.count > 1 && indexPath.item == 0) {
        cell.playBtn.hidden = NO;
    } else {
        cell.playBtn.hidden = YES;
    }
    
    XMWeakSelf(self);
    cell.itemPlayBlock = ^(XMUserDetailBannerCollectionCell * _Nonnull collectionViewcell, UIButton * _Nonnull button) {
        XMStrongSelf(self);
        self.playButton = button;
        [self playVideo];
    };
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.cIndex = scrollView.contentOffset.x / kScreenWidth;
    if (self.collectionView.contentOffset.x / kScreenWidth == 0 && self.isPlaying) {
//        self.locationLabel.hidden = YES;
    }else {
//        self.locationLabel.hidden = NO;
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.locationLabel.text = [NSString stringWithFormat:@"%.0f/%ld",scrollView.contentOffset.x / kScreenWidth + 1.0, self.dataArray.count];
    if (scrollView.contentOffset.x / kScreenWidth == 0 && self.isPlaying) {
//        self.locationLabel.hidden = YES;
    }else {
//        self.locationLabel.hidden = NO;
    }
//    if (self.bannerViewScrollerBlock) {
//        self.bannerViewScrollerBlock([[NSString stringWithFormat:@"%.0f",scrollView.contentOffset.x / kScreenWidth] integerValue]);
//    }
}

- (void)setIsPlaying:(BOOL)isPlaying {
    self.playButton.hidden = isPlaying;
    self.locationLabel.hidden = isPlaying;
    if (self.cIndex == 0) {
        if (self.isPlaying) {
            self.locationLabel.hidden = YES;
        } else {
            self.locationLabel.hidden = NO;
        }
    } else {
        self.playButton.hidden = YES;
        self.locationLabel.hidden = NO;
    }
}

- (void)playVideo {
    if (self.voiceView.isPlaying) {
        [self.voiceView stopPlay];
    }
//    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    [self startPlay];
    if (self.playBlock) {
        XMUserDetailBannerCollectionCell *cell = (XMUserDetailBannerCollectionCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
        self.playBlock(self, cell.playerView);
    }
}
- (void)startPlay {
    self.playButton.hidden = YES;
    self.locationLabel.hidden = YES;
}
- (void)endPlay {
    self.playButton.hidden = NO;
    self.locationLabel.hidden = NO;
}

- (IBAction)muteAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (self.muteBlock) {
        self.muteBlock(sender);
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (XMBannerUserInfoView *)userInfoView {
    if (_userInfoView == nil) {
        _userInfoView = [XMBannerUserInfoView viewFromXib];
    }
    return _userInfoView;
}

- (XMUserDetailVoiceView *)voiceView {
    if (_voiceView == nil) {
        _voiceView = [XMUserDetailVoiceView viewFromXib];
    }
    return _voiceView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
